package se.webbzon.boltzmann.game.object.item;

import java.nio.ByteBuffer;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.boltzmann.item.AbstractItem;
import se.webbzon.boltzmann.item.ItemGroup;
import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldeditor.WorldStaticObjectDeflation;

public class GameItemContainerDeflation extends WorldStaticObjectDeflation {

	private AbstractItem item;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new empty game item container deflation.**/
	public GameItemContainerDeflation(int deflationProperties) {
		super(deflationProperties,null);
	}
	
	/** Creates a new game item container deflation from an existing
	 * game item container.**/
	public GameItemContainerDeflation(	int deflationProperties, 
										GameItemContainer container) {
		super(deflationProperties,null,container);
		item = container.getItem();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		final int itemId = buff.getInt();
		if (itemId == -1)
			item = null;
		else {
			AbstractItem it = ItemGroup.instance().getItem(itemId);
			if (it == null)
				item = null;
			else
				item = (AbstractItem) it.clone();
		}
	}

	@Override public ByteBuffer deflate(int allocate) {
		ByteBuffer buff = super.deflate(allocate + 4);
		
		final int itemId;
		if (item == null)
			itemId = -1;
		else
			itemId = ItemGroup.instance().getId(item);
		buff.putInt(itemId);
		
		return buff;
	}
	
	/*============================================================
	Friendly Methods
	============================================================*/
	
	/** Creates a new game item container from this game item container
	 * deflation. **/
	@Friendly GameItemContainer createContainer(AbstractEnvironment env) {
		GameItemContainer container = new GameItemContainer(env,item);
		container.setPosition(getX(), getY(), getZ());
		container.setSize(getWidth(), getHeight(), getDepth());
		container.setRotation(getEX(), getEY(), getEZ(), getAngle());
		return container;
	}

}
